<?php
/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */
namespace app\mall\controller\admin;;

use app\common\builder\FormBuilder;
use app\common\builder\SortBuilder;
use app\common\builder\ListBuilder;
class MallBookmark extends MallAdmin
{
    protected $Model;

    public function initialize()
    {
        parent::initialize();
        $this->Model = model('MallBookmark');
    }

    public function Index($page = 1, $r = 20)
    {
        $map = [];
        //关键字搜索
        $keyword = input('keyword');
        if (!empty($keyword)) {
            $map['title'] = ['like', '%' . $keyword . '%'];
        }

        $status = input('status');
        if (!empty($status)) {
            $map['status'] = $status;
        }
        //时间搜索
        $start_time = input('create_time');
        if (!empty($start_time)) {
            $start_time_arr = explode('~',$start_time);
            list($start_one,$start_end)=\yunbao\time::getSomeday($start_time_arr[0]);
            list($end_one,$end_end)=\yunbao\time::getSomeday($start_time_arr[1]);
            $map['create_time']=['between',[$start_one,$end_end]];
        }

        list($list, $pagehtml) = $this->Model->where($map)->pagedata($r,false,[],['user_name','category_name','goods_name']);



        //查询关联数据，此逻辑需要优化
        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['title'] = $item['category_name'].'-'.$item['goods_name'];
                // $item['name'] = get_info_title('module/table', $item['id'], 'title', '不存在');
            }
        }
        $builder = new ListBuilder();

        //处理标题和按钮
        $builder->setMetaTitle('商品收藏管理')
//                ->addTopButton('addnew', ['href' => url_admin('edit',$map)])
                //->addTopButton('self', ['title' => '排序', 'href' => url_admin('nest', $map)])
                ->addSearchItem('status', 'select', '状态', '状态', ['1' => '正常', '0' => '已禁用', '-1' => '已删除'])
                ->addSearchItem('create_time', 'dateranger', '发布时间', '发布时间')
                ->addSearchItem('keyword', 'text', '关键字', '关键字')
                ->setTableDataList($list)
                ->setTableDataPage($pagehtml)
            //->addRightButton('self', ['title' => '管理', 'href' => url_admin('module/controller/action', ['id' => '__data_id__'])])
                ->addRightButton('info', ['name' => 'info', 'title' => '查看', 'href' => url_admin('info', ['id' => '__data_id__'])])
                ->addRightButton('edit', ['href' => url_admin('edit', ['id' => '__data_id__'])])
                ->addRightButton('delete', ['model' => 'mall/MallBookmark']);

        //处理字段
                $builder->addTableColumn('id', '编号','id');
                $builder->addTableColumn('user_name', '用户','uid');
                $builder->addTableColumn('category_name', '分类','foreign_id');
                $builder->addTableColumn('goods_name', '商品','foreign_id');
                $builder->addTableColumn('status', '状态','toggle');
                $builder->addTableColumn('create_time', '创建时间','datetime');

//                $builder->addTableColumn('title', '标题','text');
//                $builder->addTableColumn('sort', '排序','integer');
//                $builder->addTableColumn('update_time', '更新时间','datetime');
        
        $builder->addTableColumn('right_button', '操作', 'btn');

        return $builder->display();
    }

	public function info($id = 0)
	{
        $map = [];
		$builder = new FormBuilder();

		if ($id != 0) {
			$info = $this->Model->find($id);
		} else {
			$info = $map;
		}

		//处理标题
		$builder->setMetaTitle('商品收藏')
		        ->setPostUrl(url_admin('edit'))
		        ->setTemplate('layout/ucenter/info')
		        ->setBuilder('info')
		        ->setFormData($info);

		//处理字段
				$builder->addFormItem('id', 'id', '编号');
				$builder->addFormItem('user_name', 'uid', '用户');
				$builder->addFormItem('category_name', 'foreign_id', '分类');
				$builder->addFormItem('goods_name', 'foreign_id', '商品');
                $builder->addFormItem('sort', 'integer', '排序');
                $builder->addFormItem('status_name', 'text', '状态');
				$builder->addFormItem('create_time', 'datetime', '创建时间');
				$builder->addFormItem('update_time', 'datetime', '更新时间');

//                $builder->addFormItem('title', 'text', '标题');
		
		return $builder->display();
	}

    public function edit($id = 0)
    {
        $map = [];
        if (request()->isPost()) {
            $data = input('');
            $goods_info = model('MallGoods')->find($data['goods_id']);
            if( ! $goods_info) {
                $this->error('商品不存在');
            }
            $data['category_id'] = $goods_info['category_id'];

            if ($id != 0) {
                if ($this->Model->allowField(true)->save($data, ['id' => $id])) {
                    $this->success('操作成功', url_admin('index',$map));
                } else {
                    $this->error('操作失败');
                }
            } else {

                if ($this->Model->allowField(true)->save($data)) {
                    $this->success('操作成功', url_admin('index',$map));
                } else {
                    $this->error('操作失败');
                }
            }
        } else {
            $builder = new FormBuilder();

            if ($id != 0) {
                $info = $this->Model->find($id);
            } else {
                $info = $map;
            }

            //处理标题
            $title_info = $id>0?'编辑':'新增';
            $builder->setMetaTitle('商品收藏'.$title_info)
                    ->setPostUrl(url_admin('edit'))
                    ->setFormData($info);

            //处理字段
                        $builder->addFormItem('id', 'id', '编号');
                        $builder->addFormItem('user_id', 'uid', '用户');
                        $builder->addFormItem('goods_id', 'foreign_id', '商品', '', \yunbao\modelhelp::get_foreign_list('mall/mallGoods'));
                        $builder->addFormItem('status', 'toggle', '状态');

//                        $builder->addFormItem('sort', 'integer', '排序');
//                        $builder->addFormItem('title', 'text', '标题');
//                        $builder->addFormItem('create_time', 'datetime', '创建时间');
//                        $builder->addFormItem('update_time', 'datetime', '更新时间');
            
            return $builder->display();
        };
    }

    public function del($id = 0)
    {
        $id = array_unique((array)input('id', 0));

        if (empty($id)) {
            $this->error(lang('_ERROR_DATA_SELECT_') . lang('_EXCLAMATION_'));
        }

        $map = array('id' => array('in', $id));
        if ($this->Model->where($map)->delete()) {

            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
}